package com.atguigu.gulimall.coupon.service.impl;

import com.atguigu.common.to.MemberPrice;
import com.atguigu.common.to.SkuReductionTo;
import com.atguigu.gulimall.coupon.entity.MemberPriceEntity;
import com.atguigu.gulimall.coupon.entity.SkuLadderEntity;
import com.atguigu.gulimall.coupon.service.MemberPriceService;
import com.atguigu.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.coupon.dao.SkuFullReductionDao;
import com.atguigu.gulimall.coupon.entity.SkuFullReductionEntity;
import com.atguigu.gulimall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private SkuFullReductionService skuFullReductionService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveinfo(SkuReductionTo skuReductionTo) {
        //4.sku的优惠,满减等信息:
        //gulimall_sms->sms_sku_ladder/sms_sku_full_reduction/sms_member_price
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity()
                .setFullCount(skuReductionTo.getFullCount())
                .setDiscount(skuReductionTo.getDiscount())
                .setAddOther(skuReductionTo.getCountStatus())
                .setSkuId(skuReductionTo.getSkuId());
        if (skuLadderEntity.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);

        }
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity()
                .setFullPrice(skuReductionTo.getFullPrice())
                .setReducePrice(skuReductionTo.getReducePrice())
                .setAddOther(skuReductionTo.getFullCount())
                .setSkuId(skuReductionTo.getSkuId());
        if (BigDecimal.ZERO.compareTo(skuFullReductionEntity.getFullPrice()) < 0) {
            this.save(skuFullReductionEntity);
        }
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        if (!CollectionUtils.isEmpty(memberPrice)) {
            List<MemberPriceEntity> priceEntityList = memberPrice.stream().map(mp -> new MemberPriceEntity()
                    .setMemberLevelId(mp.getId())
                    .setMemberLevelName(mp.getName())
                    .setMemberPrice(mp.getPrice())
                    .setAddOther(skuReductionTo.getPriceStatus())
                    .setSkuId(skuReductionTo.getSkuId())
            ).filter(mp -> BigDecimal.ZERO.compareTo(mp.getMemberPrice()) < 0)
                    .collect(Collectors.toList());
            memberPriceService.saveBatch(priceEntityList);
        }
    }

}